package com.lunhui.user.provider.impl;

import com.alibaba.fastjson.JSONObject;
import com.lunhui.user.business.RoseService;
import com.lunhui.user.client.RoseClient;
import com.lunhui.user.client.model.RoseDto;
import com.lunhui.user.dal.model.Rose;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author renminghe-kg
 */

@Service
public class RoseClientImpl implements RoseClient {
    private static final Logger logger = LoggerFactory.getLogger(RoseClientImpl.class);

    @Autowired
    @Qualifier("roseService")
    private RoseService roseService;

    @Override
    public List<RoseDto> findRoseIdAndNameListByUserId(Integer userId) {
        try {
            List<Rose> roseList = roseService.findRoseIdAndNameListByUserId(userId);

            if (roseList != null) {
                List<RoseDto> roseDtoList = new LinkedList<>();

                boList2DtoList(roseList, roseDtoList);

                logger.info("通过userId获取角色名列表数据: " + JSONObject.toJSONString(roseDtoList));

                return roseDtoList;
            } else {
                return null;
            }

        } catch (Exception e) {
            logger.error("查询角色信息列表失败: " + e.getMessage());
            throw new RuntimeException("查询角色信息列表失败: " + e.getMessage());
        }
    }

    @Override
    public Integer saveToDB(RoseDto roseDto) {
        try {
            Rose rose = new Rose();
            rose.setRoseLevel(1);
            rose.setRoseExpress(0L);
            rose.setRoseLastAreaId(0);
            rose.setRoseLastSceneId(0);
            rose.setRoseLastPositionX(1);
            rose.setRoseLastPositionY(1);
            rose.setRoseCurrentHp(100L);
            rose.setRoseMaxHp(100L);
            rose.setRoseCurrentMp(100L);
            rose.setRoseMaxMp(100L);
            rose.setSupplyCount(100L);
            rose.setRoseDefense(10L);
            rose.setRoseAttack(90L);
            rose.setRoseAgility(10L);

            BeanUtils.copyProperties(roseDto, rose);

            logger.info("保存角色信息： " + rose);

            return roseService.save(rose);
        } catch (Exception e) {
            logger.error("保存角色信息失败: " + e.getMessage());
            throw new RuntimeException("保存角色信息失败: " + e.getMessage());
        }
    }

    @Override
    public RoseDto findRoseByUserIdAndRoseName(Integer userId, String roseName) {
        try {
            Rose rose = roseService.findRoseByUserIdAndRoseName(userId, roseName);

            if (rose != null) {
                RoseDto roseDto = new RoseDto();
                BeanUtils.copyProperties(rose, roseDto);

                return roseDto;
            }
        } catch (Exception e) {
            logger.error("通过userId和RoseName获取角色信息失败: " + e);
        }

        return null;
    }

    @Override
    public RoseDto findRoseByRoseId(Integer roseId) {
        try {
            Rose rose = roseService.findRoseByRoseId(roseId);

            if (rose != null) {
                RoseDto roseDto = new RoseDto();
                BeanUtils.copyProperties(rose, roseDto);

                return roseDto;
            }
        } catch (Exception e) {
            logger.error("通过roseId获取角色信息失败: " + e);
        }

        return null;
    }

    @Override
    public Integer update(RoseDto roseDto) {
        try {
            Rose rose = new Rose();
            BeanUtils.copyProperties(roseDto, rose);

            roseService.update(rose);
            return 1;
        } catch (Exception e) {
            logger.error("更新角色信息失败: " + e);
        }

        return -1;
    }

    private void boList2DtoList (List<Rose> roses, List<RoseDto> roseDtos) {
        roses.forEach((entity) -> {
            RoseDto roseDto = new RoseDto();
            BeanUtils.copyProperties(entity, roseDto);

            roseDtos.add(roseDto);
        });

    }
}